package com.kaishengit.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.kaishengit.entity.Goods;
import com.kaishengit.util.DBHelp;
import com.kaishengit.util.RowMapper;

public class GoodsDao {

	private DBHelp<Goods> db = new DBHelp<Goods>();
	public void save(Goods goods) {
		String sql = "insert into t_goods (id,number,name,price,enable) values (?,?,?,?,?)";
		db.executeSQL(sql,goods.getId(),goods.getNumber(),goods.getName(),goods.getPrice(),goods.isEnable());
		System.out.println(goods.getName());
	}
	public List<Goods> findAll() {
		String sql = "select id,number,name,price,enable from t_goods";
		return db.executeQueryForList(sql, new GoodsRowMapper());
		
	}
	private class GoodsRowMapper implements RowMapper<Goods>{

		public Goods mapperRow(ResultSet rs) throws SQLException {

			Goods goods = new Goods();
			
			goods.setId(rs.getString("id"));
			goods.setNumber(rs.getString("number"));
			goods.setName(rs.getString("name"));
			goods.setPrice(rs.getFloat("price"));
			goods.setEnable(rs.getBoolean("enable"));
			
			return goods;
			
		}
		
		
	}
	public Goods findById(String id) {
		String sql = "select id,number,name,price,enable from t_goods where id = ?";
		return db.executeQueryForObject(sql, new GoodsRowMapper(), id);
		
	}
	public void update( ) {
		String sql = "update t_prod set prodName = ?,price = ? where id = ?";
		db.executeSQL(sql);
		
	}
	public void del(String id) {
		String sql = "delete from t_goods where id = ?";
		db.executeSQL(sql,id);
		
	}

}
